package com.nf;

import com.nf.Mapper.ProductMapper;
import com.nf.pojo.Product;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * ClossName:HelloWord
 * Pakage:com.nf
 * Description
 *
 * Mybatis快速入门
 *
 * Resources : Mybatis一个加载类，里面有一个静态方法 ==》getResourceAsStream() 传入一个字符串
 *
 * InputStream : 字节输入流
 *
 * new SqlSessionFactoryBuilder()对象，通过build()的方法 传入一个流
 *
 * SqlSessionFactory : 对象
 *
 * 代理开发
 *
 * @Author L
 * @Create 2023/11/29 19:15
 * @Version 2023.1.3
 */
public class Mybatis2 {

    /*
        查询所有
     */
    @Test
    public void selectAll() throws IOException {
        //1）加载Mybatis核心配置文件，获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2)获取SqlSession对象，用它执行sql
        SqlSession sqlSession =  sqlSessionFactory.openSession();

        //3)执行sql
        //List<Product> product = sqlSession.selectList("test.selectAll");

        //获取ProductMapper接口代理对象
        ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);


        List<Product> product = productMapper.selectAll();


        System.out.println(product);

        //4)释放资源
        sqlSession.close();
    }
    /*
        根据三个条件查询
     */
    @Test
    public void selectInfo() throws IOException {

        //获得参数
        String name = "汤";
        String type = "现";
        double price = 99;

        //处理参数
        name ="%"+name+"%";
        type ="%"+type+"%";




        //1）加载Mybatis核心配置文件，获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2)获取SqlSession对象，用它执行sql
        SqlSession sqlSession =  sqlSessionFactory.openSession();

        //获取ProductMapper接口代理对象
        ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);


        //第三种方法
        Map map = new HashMap();
        map.put("goodsPrice",price);
        map.put("goodsName",name);
        map.put("goodsType",type);

        List<Product> list = productMapper.selectInfo(map);
        System.out.println(list);

        //4)释放资源
        sqlSession.close();
    }

    /*
        单条件查询
     */
    @Test
    public void selectInfoOne() throws IOException {

        //获得参数
        String name = "汤";
        //  String type = "快";
        //double price = 99;

        //处理参数
         name = "%"+name+"%";
        // type = "%"+type+"%";


        //1）加载Mybatis核心配置文件，获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2)获取SqlSession对象，用它执行sql
        SqlSession sqlSession =  sqlSessionFactory.openSession();


        //获取ProductMapper接口代理对象
        ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);


        //第三种方法
        Map map = new HashMap();
        map.put("goodsName",name);
        // map.put("type",type);
       // map.put("price",price);
        List<Product> list = productMapper.selectInfo(map);
        System.out.println(list);

        //4)释放资源
        sqlSession.close();
    }


    /*
        根据id查询
     */
    @Test
    public void selectById() throws IOException {
        //1）加载Mybatis核心配置文件，获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2)获取SqlSession对象，用它执行sql
        SqlSession sqlSession =  sqlSessionFactory.openSession();

        //3)执行sql
        //List<Product> product = sqlSession.selectList("test.selectAll");

        //获取ProductMapper接口代理对象
        ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);



        Product product = productMapper.selectById(1);

        System.out.println(product);

        //4)释放资源
        sqlSession.close();
    }


    /*
       添加方法
    */
    @Test
    public void addproduct() throws IOException {

        //获得参数
        String name = "火腿";
        String iages = "default.png";
        double price = 77;
        String type = "快餐";
        String specification ="500克";


        //封装对象
        Product product = new Product();
        product.setGoodsName(name);
        product.setGoodsImage(iages);
        product.setGoodsPrice(price);
        product.setGoodsType(type);
        product.setGoodsSpecification(specification);


        //1）加载Mybatis核心配置文件，获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2)获取SqlSession对象，用它执行sql
        SqlSession sqlSession =  sqlSessionFactory.openSession();

        //获取ProductMapper接口代理对象
        ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);


        //执行方法
        int rows = productMapper.addProduct(product);
        System.out.println("大于0表示数据添加成功:"+rows);
        //获取主键
        Integer id = product.getGoodsId();
        System.out.println("主键为:"+id);

        //提交事物
        sqlSession.commit();

        //4)释放资源
        sqlSession.close();
    }


    /*
          修改方法
       */
    @Test
    public void updateproduct() throws IOException {

        //获得参数
        int id = 17;
        String name = "北京烤鸭";
       // String iages = "default.png";
       // double price = 199;
       // String type = "烧烤";
        //String specification ="500克";


        //封装对象
        Product product = new Product();
        product.setGoodsId(id);
        product.setGoodsName(name);
//        product.setGoodsImage(iages);
//        product.setGoodsPrice(price);
//        product.setGoodsType(type);
//        product.setGoodsSpecification(specification);


        //1）加载Mybatis核心配置文件，获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2)获取SqlSession对象，用它执行sql
        SqlSession sqlSession =  sqlSessionFactory.openSession();

        //获取ProductMapper接口代理对象
        ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);


        //执行方法
        int rows = productMapper.updateProduct(product);
        System.out.println("大于0表示数据修改成功:"+rows);


        //提交事物
        sqlSession.commit();

        //4)释放资源
        sqlSession.close();
    }


    /*
           删除方法
       */
    @Test
    public void deleteProduct() throws IOException {

        //获得参数
        int id = 17;


        //1）加载Mybatis核心配置文件，获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2)获取SqlSession对象，用它执行sql
        SqlSession sqlSession =  sqlSessionFactory.openSession();

        //获取ProductMapper接口代理对象
        ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);


        //执行方法
        int rows = productMapper.deleteProduct(id);
        System.out.println("大于0表示数据删除成功:"+rows);

        //提交事物
        sqlSession.commit();

        //4)释放资源
        sqlSession.close();
    }

    /*
             批量删除方法
          */
    @Test
    public void deleteProducts() throws IOException {

        //获得参数
        int[] id = {16,12};


        //1）加载Mybatis核心配置文件，获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2)获取SqlSession对象，用它执行sql
        SqlSession sqlSession =  sqlSessionFactory.openSession();

        //获取ProductMapper接口代理对象
        ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);


        //执行方法
        int rows = productMapper.deleteProducts(id);
        System.out.println("大于0表示数据删除成功:"+rows);

        //提交事物
        sqlSession.commit();

        //4)释放资源
        sqlSession.close();
    }




}
